/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.processor.business;

import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.component.gateway.dianlan.annotations.DianlanProtocolParse;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.order.DianlanLoginOrder;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.result.DianlanLoginResult;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanFrameSign;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanLoginResultType;
import info.peigen.hotpot.component.gateway.dianlan.processor.AbstractDianlanProcessor;
import info.peigen.hotpot.component.gateway.dianlan.protocol.Dianlan;
import info.peigen.hotpot.component.gateway.dianlan.protocol.active.DianlanBillingModelSetting;
import info.peigen.hotpot.component.gateway.dianlan.protocol.passive.DianlanLogin;
import info.peigen.hotpot.component.gateway.dianlan.protocol.passive.DianlanLoginResponse;
import info.peigen.hotpot.component.gateway.dianlan.util.DianlanUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioSession;

/**
 * <b>(DianlanLoginProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/23
 */
@Component("dianlanLoginProcessor")
@Slf4j
public class DianlanLoginProcessor<T> extends AbstractDianlanProcessor implements MessageProcessor<T> {

    @Override
    public void process(AioSession session, Object obj) {
        Dianlan      dianlan = (Dianlan) obj;
        DianlanLogin login   = DianlanProtocolParse.deserialization(dianlan.getData(), DianlanLogin.class);
        log.info("Data:" + JSON.toJSONString(login));
        DianlanLoginOrder order = DianlanLoginOrder.builder()
                .sessionId(session.getSessionID())
                .stubNo(login.getStubNo())
                .build();

        DianlanLoginResult   result = dianlanServiceFacade.login(order);
        DianlanLoginResponse response;
        if (result.success()) {
            response = DianlanLoginResponse.builder().stubNo(login.getStubNo()).loginResult(DianlanLoginResultType.Success).build();
        } else {
            response = DianlanLoginResponse.builder().stubNo(login.getStubNo()).loginResult(DianlanLoginResultType.Failure).build();
        }
        dianlan.setFrameSign(DianlanFrameSign.LoginResponse);
        response(dianlan, response);
        write(session, dianlan);

        // 设置费用
        DianlanBillingModelSetting setting = DianlanBillingModelSetting.builder()
                .stubNo(login.getStubNo())
                .billingModelCode("0100")
                .countOfLoss("00")
                .highestElectricCharge("400D0300")
                .highestServiceCharge("400D0300")
                .highElectricCharge("400D0300")
                .highServiceCharge("400D0300")
                .flatElectricCharge("400D0300")
                .flatServiceCharge("400D0300")
                .lowestElectricCharge("400D0300")
                .lowestServiceCharge("400D0300")
                .build();
        dianlan = Dianlan.builder().serialNumber("0025")
                .frameSign(DianlanFrameSign.BillingModelSetting).build();
        DianlanUtil.response(dianlan, setting);
        DianlanUtil.write(session, dianlan);
    }

    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        MessageProcessor.super.stateEvent(session, stateMachineEnum, throwable);
    }
}